package com.itqf.controller;

import com.itqf.pojo.Address;
import com.itqf.pojo.User;
import com.itqf.service.AddressService;
import com.itqf.service.impl.AddressServiceImpl;
import com.itqf.utils.Constants;
import com.itqf.vo.R;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * projectName: gpshop
 *
 * @author: 赵伟风
 * time: 2021/7/22 10:16
 * description:
 */
@WebServlet("/address/*")
public class AddressController extends BaseServlet {


    /**
     * 添加地址
     *    1.将地址数据保存到数据!
     *    2.地址关联了用户数据!指明是哪个用户的地址!
     *
     * 思考:
     *    有一个默认地址的概念!
     *    什么是默认地址: 当你没次提交订单的时候,默认选择中地址!
     *
     * 如何设置默认地址:
     *    1.展示地址页面有设置默认地址的按钮!
     *    2.如果用户没有对应的地址,第一次添加!那么这个地址就是默认地址!
     *
     * 地址添加成功:
     *    回到self_info.jsp页面展示地址数据!
     *
     *
     * 只有在做混合式开发[jsp / freemarker threamflmf] [共享域] 后台[接口 + 前端]:才会使用转发或者重定向去串联多个动作!
     *       添加 + 展示 =  添加的功能
     * 前后端分离: 后台 接口  前端 展示 两个接口  添加接口  + 查询接口
     *
     * @param request
     * @param response
     * @return
     */
    public String save(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //1.获取请求参数
        Map<String, String[]> map = request.getParameterMap();

        //2.调用保存地址业务
        AddressService addressService = new AddressServiceImpl();
        R r = addressService.saveAddress(map);

        //3.响应数据
        if (r.getCode() == 0) {
            //失败的提示语
            request.setAttribute("msg", r.getMsg());
            return  Constants.FORWARD_ACTION + "/self_info.jsp";
        }

        //添加成功,我们转发到显示的方法中!
        return Constants.FORWARD_ACTION + "/address/show";
    }


    /**
     * 展示地址
     *
     *     我们需要什么条件?
     *     需要用户uid!
     *
     *     查询用户对应的地址集合即可!
     *
     * @param request
     * @param response
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    public String show(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //1.获取用户id
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            session.setAttribute("msg", "查询地址,必须在登录的情况下!");
            return Constants.REDIRECT_ACTION + request.getContextPath() + "/login.jsp";
        }

        //2.查询用户对应的地址
        AddressService addressService = new AddressServiceImpl();
        List<Address> addresses = addressService.findAll(user.getuId());

        //3.放入共享域

        request.setAttribute("addresses",addresses);


        return Constants.FORWARD_ACTION + "/self_info.jsp";
    }


    /**
     * 设置默认地址接口
     *
     *    1.前端传递需要设置默认地址aId
     *    2.取消所有默认地址 当期的用户
     *    3.指定aId设置为默认地址即可!
     *    4.成功 show展示
     *      失败 show展示
     *
     * @param request
     * @param response
     * @return
     * @throws IllegalAccessException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    public String defualt(HttpServletRequest request, HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        //1.获取用户id
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        if (user == null) {
            session.setAttribute("msg", "查询地址,必须在登录的情况下!");
            return Constants.REDIRECT_ACTION + request.getContextPath() + "/login.jsp";
        }

        //2.查询用户对应的地址

        String aId = request.getParameter("aId");

        AddressService addressService = new AddressServiceImpl();
        R r = addressService.setDefualt(user.getuId(),aId);

        //3.放入共享域

        if (r.getCode() == 0) {

            request.setAttribute("msg", r.getMsg());
        }

        return Constants.FORWARD_ACTION + "/address/show";
    }



}
